package com.lzb.note.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzb.note.entity.Notes;
import com.lzb.note.entity.User;
import com.lzb.note.service.NotesService;
import com.lzb.note.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Notes)表控制层
 *
 * @author makejava
 * @since 2021-05-16 13:45:33
 */
@RestController
@RequestMapping("notes")
@Api(value = "笔记类")
public class NotesController {
    /**
     * 服务对象
     */
    @Resource
    private NotesService notesService;

    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "检索单个目标", notes = "")
    @GetMapping("selectOne")
    public ModelAndView selectOne(String id) {
        Notes notes = this.notesService.queryById(id);
        JSONObject json = new JSONObject();
        if(notes==null){
            json.put("title","此分享错误，请输入正确连接!!");
            json.put("user","无效用户");
            json.put("content","无效连接");
        }else {
            String content = notes.getContent();
            json.put("title",content.substring(0,content.indexOf("##")));
            json.put("user",notes.getUserid());
            json.put("content",content.substring(content.indexOf("##")+2));
        }
        return new ModelAndView("index","note",json);
    }

    @ApiOperation(value = "添加单个目标", notes = "")
    @PostMapping(value = "addNote",produces = "application/json;charset=UTF-8")
    public Notes addNote(@RequestBody Notes notes) {
        return this.notesService.insert(notes);
    }

    /**
     * 上传接口呀
     * @param notes
     * @return
     */
    @ApiOperation(value = "上传接口", notes = "")
    @PostMapping(value = "addAllChangeListNote",produces = "application/json;charset=UTF-8")
    public boolean addAllChangeListNote(@RequestBody List<Notes> notes) {
        notes.forEach(x->{
            this.notesService.insert(x);
        });
        return true;
    }
    @ApiOperation(value = "删除接口", notes = "")
    @PostMapping(value = "delNote",produces = "application/json;charset=UTF-8")
    public boolean delNote(@RequestBody Notes notes) {
        return this.notesService.deleteById(notes.getId());
    }
    @ApiOperation(value = "更新接口", notes = "")
    @PostMapping(value = "updateNote",produces = "application/json;charset=UTF-8")
    public Notes updateNote(@RequestBody Notes notes) {
        return this.notesService.update(notes);
    }
    @ApiOperation(value = "查询接口", notes = "")
    @PostMapping(value = "selectNote",produces = "application/json;charset=UTF-8")
    public List<Notes> queryAll(@RequestBody Notes note) {
        //notechange 对客户端来说 0：已同步 1：未同步  对服务端来说 0：未同步 1：已同步
        //给客户端未从服务器上拿的
        List<Notes> notes = this.notesService.queryAll(note);
        return notes;
    }
    /**
     * 带用户校验接口
     *
     * @param note 主键
     * @return 所有数据
     */
    @ApiOperation(value = "带用户校验接口", notes = "")
    @PostMapping(value = "selectAll",produces = "application/json;charset=UTF-8")
    public List<Notes> selectAll(@RequestHeader("user") String user,@RequestBody Notes note) {
        User userObj = new User();
        userObj.setUsername(user);
        List<User> users = this.userService.queryAll(userObj);
        if(CollectionUtils.isEmpty(users)){
           return null;
        }
        return this.notesService.queryAll(note);
    }
}